package com.example.vv.model.projectItem;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.vv.R;
import com.example.vv.adapter.BaseRecyclerViewAdapter;
import com.example.vv.adapter.RecyclerViewHolder;
import com.example.vv.fragmentWork.entity.ProjectListEntity;
import com.example.vv.fragmentWork.mvp.MvpFragment;
import com.example.vv.model.account.base.adapter.AvAdapter;
import com.example.vv.model.accountItem.base.adapter.AIAdapter;
import com.example.vv.model.projectItem.base.adapter.PIAdapter;
import com.example.vv.model.projectItem.base.contract.PIContract;
import com.example.vv.model.projectItem.base.presenter.PIPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import butterknife.BindView;


public class PIFragment extends MvpFragment<PIContract.View, PIPresenter> implements PIContract.View {

    @BindView(R.id.pi_recycler)
    RecyclerView piRecycler;
    int id;
    @BindView(R.id.pi_smartRefresh)
    SmartRefreshLayout piSmartRefresh;
    int page = 1;
    PIAdapter piAdapter;
    public PIFragment(int id) {
        this.id = id;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_p_i;
    }

    @Override
    public PIPresenter initPresenter() {
        return new PIPresenter();
    }

    @Override
    public void initView() {
        piSmartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page+=1;
                presenter.setProjectList("project/list/"+page+"/json?cid=" + id);
                piSmartRefresh.finishLoadMore(1000);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (piAdapter!=null) {
                    piAdapter.clear();
                    presenter.setProjectList("project/list/1/json?cid=" + id);
                    page=1;
                }
                piSmartRefresh.finishRefresh(1000);
            }
        });
        piAdapter = new PIAdapter(getActivity());
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        piRecycler.setLayoutManager(manager);
        piRecycler.setAdapter(piAdapter);
    }

    @Override
    public void lazyLoad() {
    }

    @Override
    public void initData() {
        piSmartRefresh.autoRefresh();
        presenter.setProjectList("project/list/1/json?cid=" + id);
    }

    @Override
    public void doBusiness() {

    }

    @Override
    public View titleBar() {
        return null;
    }


    @Override
    public Context getMContext() {
        return getActivity();
    }


    @Override
    public void getProjectList(List<ProjectListEntity.DatasBean> projectList) {
        piAdapter.addData(projectList);
    }
}